1. Repeating Setup For Many Tests

If you have some work you need to do repeatedly for many tests, you can use beforeEach and afterEach.
there is a Counter Class for test like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export default class Counter {
constructor() {
this.number = 0
}
addOne() {
this.number += 1
}

minusOne() {
this.number -= 1
}
addTwo() {
this.number += 2
}

minusTwo() {
this.number -= 2
}
}

you might repeat to create the instance for counter, so we can use beforeEach to prepare.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Counter from "./counter"
let counter = null
beforeEach(() => {
console.log("beforeEach")
counter = new Counter()
})
afterEach(() => {
console.log("afterEach")
})
test("test counter addOne", () => {
console.log("test counter addOne")
counter.addOne()
expect(counter.number).toBe(1)
})

2. Scoping && Order of execution of describe and test blocks

By default, the before and after blocks apply to every test in a file. You can also group tests together using a describe block. When they are inside a describe block, the before and after blocks only apply to the tests within that describe block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import Counter from "./counter"
// describe block Applies to all tests in this file
describe("counter js test", () => {
let counter = null;
console.log("counter js test")
beforeAll(() => {
console.log("beforeAll")
})

beforeEach(() => {
console.log("beforeEach")
counter = new Counter();
})

afterEach(() => {
console.log("afterEach")
})

afterAll(() => {
console.log("afterAll")
})

describe("counter js test add", () => {
console.log("counter js test add")
beforeAll(() => {
console.log("beforeAll add")
})
beforeEach(() => {
console.log("beforeEach add")
})

afterEach(() => {
console.log("afterEach add")
})

afterAll(() => {
console.log("afterAll add")
})
test("test counter addOne", () => {
console.log("test counter addOne")
counter.addOne()
expect(counter.number).toBe(1)
})
})

describe("counter js test minus", () => {
// // Applies only to tests in this describe block
console.log("counter js test minus")
test("test counter minusOne", () => {
console.log("counter js test minusOne")
counter.minusOne()
expect(counter.number).toBe(-1)
})
test("test counter minusTwo", () => {
console.log("counter js test minusTwo")
counter.minusTwo()
expect(counter.number).toBe(-2)
})
})
})

// execute order
/**
* counter js test
* counter js test add
* counter js test munis
* beforeAll
* beforeAll add
* beforeEach
* beforeEach add
* test counter addOne
* afterEach add
* afterEach
* afterAll add
* beforeEach
* counter js test minusOne
* afterEach
* beforeEach
* counter js test minusTwo
* afterEach
*/

3. General Advice

  • If a test is failing, one of the first things to check should be whether the test is failing when it’s the only test that runs. In Jest it’s simple to run only one test - just temporarily change that test command to a test.only

  • If you have a test that often fails when it’s run as part of a larger suite, but doesn’t fail when you run it alone, it’s a good bet that something from a different test is interfering with this one. You can often fix this by clearing some shared state with beforeEach. If you’re not sure whether some shared state is being modified, you can also try a beforeEach that just logs data.